﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;

public partial class SearchModify : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		String strUID = OperateControl.PickupAvaiableUserID(this);
		if (!IsPostBack) {
			operaterLabel.Text = strUID;

			ControlHideDisable();
			String strBarCode = Request.QueryString["ArticleId"];
			String strSrcFile = Request.QueryString["SrcFile"];
			if (strBarCode != null && strSrcFile != null)
			{
				tbBarCode.Text = strBarCode;
				tbBarCode.Enabled = false;
				btnSearch.Enabled = false;

				doSearch(strSrcFile);
			}
		}
	}
	private	void	ControlHideDisable()
	{
		trPassword.Visible = false;
		trApplyTime.Visible = false;
		trMDName.Visible = false;
		trMDAddress.Visible = false;
		trName.Visible = false;
		trPhone.Visible = false;
		trAddress.Visible = false;
		trComment.Visible = false;
		trSendTime.Visible = false;
		trDanHao.Visible = false;

		lblSaveInfo.Text = "";

		tbPkupGoods.Visible = false;
		tbCancel.Visible = false;
		btnModify.Visible = false;

		tbPassword.Enabled = false;
		tbName.Enabled = false;
		tbPhone.Enabled = false;
		tbAddress.Enabled = false;
		tbComment.Enabled = false;
		tbDanHao.Enabled = false;

		Label1.Text = "";
		Label2.Text = "";

		tbKind.Text = "";
		tbSingleMoney.Text = "";

		tbPassword.Text = "";

		tbApplyTime.Text = "";
		tbMDName.Text = "";
		tbMDAddress.Text = "";

		tbName.Text = "";
		tbPhone.Text = "";
		tbAddress.Text = "";
		tbComment.Text = "";

		tbSendTime.Text = "";
		tbDanHao.Text = "";
	}
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		ControlHideDisable();

		doSearch(null);
	}
	private void doSearch(String srcLaunchFile)
	{
		string strBarCode = tbBarCode.Text.Trim();
		MsgAccessor msgAcce = new MsgAccessor();
		TiaoMaRecord tmRec = msgAcce.GetOneTiaoMaRecord(strBarCode, null);
		if (tmRec.TiaoMa.ToString() != strBarCode)
		{
			Label1.Text = @"该条码不存在！";
			return;
		}
		if (!tmRec.IsActivated)
		{
			Label1.Text = @"该礼券还未激活！";
			return;
		}
		if (tmRec.FlowID > 3)
		{
			Label1.Text = @"该礼券已经作废！";
			return;
		}
		String strUID = operaterLabel.Text;
		int nUID;
		int.TryParse(strUID, out nUID);
		MemberAccessor memberSystem = new MemberAccessor();
		UserData usrData = memberSystem.GetMemberInfo(nUID);

		CPNAccessor cpnAcce = new CPNAccessor();
		ClassData kdData = cpnAcce.GetGoodsKindInfo(tmRec.Kind);
		tbKind.Text = kdData.ClassName;
		tbSingleMoney.Text = kdData.ClassMoney.ToString();
		switch (tmRec.FlowID)
		{
			case 0:
				if (usrData.GroupId != 5 && usrData.GroupId != 1 && usrData.GroupId != 2)
				{
					Label1.Text = @"该礼券未提过货！";
				}
				else
				{
					Label1.Text = @"该礼券可以提货！";

					tbPkupGoods.Visible = true;
					if (usrData.GroupId == 5)
					{
						tbPassword.Enabled = true;
						trPassword.Visible = true;
					}
				}
				break;
			case 1:
				{
					Label1.Text = @"该礼券已经在以下门店或管理员处提过货！";
					UserData applyUser = memberSystem.GetMemberInfo(tmRec.Applier);
					if (applyUser.Introduction == null || applyUser.Introduction.Length < 1)
						tbMDName.Text = applyUser.RealName;
					else
						tbMDName.Text = applyUser.Introduction;
					tbMDAddress.Text = applyUser.Address;
					tbApplyTime.Text = tmRec.ApplyTime.ToString();
					trMDName.Visible = true;
					trMDAddress.Visible = true;
					trApplyTime.Visible = true;
					if (usrData.GroupId == 1 || usrData.GroupId == 2 || tmRec.Applier == usrData.MemberId)
					{
						DateTime curdt = DateTime.Now;
						cpnAcce.GetServerCurTime(ref curdt);
						TimeSpan ts = curdt - tmRec.ApplyTime;
						if (ts.Days < 3)
						{
							tbCancel.Visible = true;
						}
					}
				} break;
			case 2:
			case 3:
				{
					tbApplyTime.Text = tmRec.ApplyTime.ToString();
					if (tmRec.FlowID == 2)
					{
						Label1.Text = @"该礼券已经下单，等待发货！";
						if (usrData.GroupId == 1 || usrData.GroupId == 2 || tmRec.Applier == usrData.MemberId)
						{
							DateTime curdt = DateTime.Now;
							cpnAcce.GetServerCurTime(ref curdt);
							TimeSpan ts = curdt - tmRec.ApplyTime;
							if (ts.Days < 3)
							{
								tbCancel.Visible = true;
							}
						}
					}
					else
					{
						Label1.Text = @"该礼券已经发货！";
						tbSendTime.Text = tmRec.CnfmTime.ToString();
						trSendTime.Visible = true;
					}

					string strCustomer = tmRec.ConsumerInfo;
					char[] pSplit = new char[1];
					pSplit[0] = '/';
					string[] liststr = strCustomer.Split(pSplit);
					if (liststr.Length >= 3)
					{
						tbName.Text = liststr[0];
						tbPhone.Text = liststr[1];
						tbAddress.Text = liststr[2];
					}
					else if (liststr.Length == 2)
					{
						tbName.Text = liststr[0];
						tbPhone.Text = liststr[1];
					}
					else if (liststr.Length == 1)
					{
						tbName.Text = liststr[0];
					}
					tbComment.Text = tmRec.CsterComment;
					trName.Visible = true;
					trPhone.Visible = true;
					trAddress.Visible = true;
					trComment.Visible = true;

					if (tmRec.FlowID == 2 && (usrData.GroupId == 1 || usrData.GroupId == 2 || tmRec.Applier == usrData.MemberId
						|| (tmRec.Applier == 0 && usrData.GroupId == 3)))
					{
						tbName.Enabled = true;
						tbPhone.Enabled = true;
						tbAddress.Enabled = true;
						tbComment.Enabled = true;

						btnModify.Visible = true;
					}
					else if (tmRec.FlowID == 3)
					{
						tbDanHao.Text = tmRec.WLDanHao;
						trDanHao.Visible = true;

						if (usrData.GroupId == 1 || usrData.GroupId == 2 || tmRec.Confirmer == usrData.MemberId)
						{
							DateTime curdt = DateTime.Now;
							cpnAcce.GetServerCurTime(ref curdt);
							TimeSpan ts = curdt - tmRec.CnfmTime;
							if (ts.Days < 3)
							{
								tbDanHao.Enabled = true;
								btnModify.Visible = true;
							}
						}
					}
				} break;
		}
		lblSaveInfo.Text = strBarCode;
		lblSaveInfo.Text += "#";
		lblSaveInfo.Text += tmRec.FlowID.ToString();
		if (srcLaunchFile != null && srcLaunchFile.Length > 0)
		{
			lblSaveInfo.Text += "#";
			lblSaveInfo.Text += srcLaunchFile;
		}
	}
	protected void btnModify_Click(object sender, EventArgs e)
	{
		string	strSaveInfo = lblSaveInfo.Text;
		char[] pSplit = new char[1];
		pSplit[0] = '#';
		string[] liststr = strSaveInfo.Split(pSplit);
		if(liststr.Length >= 2)
		{
			String srcLaunchFile = null;
			if (liststr.Length >= 3)
				srcLaunchFile = liststr[2];

			int	nFlow = 0;
			int.TryParse(liststr[1], out nFlow);

			String strUID = operaterLabel.Text;
			int		nOprater;
			int.TryParse(strUID, out nOprater);

			string	strName = tbName.Text.Trim();
			string	strPhone = tbPhone.Text.Trim();
			string	strAddr = tbAddress.Text.Trim();
			if(strName.Length < 2 && nFlow == 2){
				Label2.Text = @"请输入提货人姓名！";
				return;
			}
			if (strPhone.Length < 7 && nFlow == 2){
				Label2.Text = @"请输入提货人电话！";
				return;
			}
			if(strAddr.Length < 2 && nFlow == 2){
				Label2.Text = @"请输入提货人地址！";
				return;
			}
			string strComment = tbComment.Text.Trim();

			string strDanHao = tbDanHao.Text.Trim();
			if(strDanHao.Length < 3 && nFlow == 3){
				Label2.Text = @"请输入物流单号！";
				return;
			}

			string	strCustomeInfo;
			strCustomeInfo = strName;
			strCustomeInfo += @"/";
			strCustomeInfo += strPhone;
			strCustomeInfo += @"/";
			strCustomeInfo += strAddr;

			MsgAccessor msgAcce = new MsgAccessor();
			int nRet = msgAcce.UpdateBarCode(nOprater, liststr[0], nFlow, strCustomeInfo, strComment, strDanHao);
			if(nRet != 20){
				Label2.Text = @"修改失败！";
			}
			else {
				if (srcLaunchFile == null || srcLaunchFile.Length < 1)
				{
					Response.Clear();
					this.ClientScript.RegisterStartupScript(GetType(), @"MODIFYBARCODE", @"<Script Language='JavaScript'>window.alert('修改成功！')</script>");
				}
				else
				{
					Response.Clear();
					Response.Redirect(srcLaunchFile);
				}
			}
		}
	}
	protected void tbPkupGoods_Click(object sender, EventArgs e)
	{
		string	strSaveInfo = lblSaveInfo.Text;
		char[] pSplit = new char[1];
		pSplit[0] = '#';
		string[] liststr = strSaveInfo.Split(pSplit);
		if (liststr.Length < 2) {
			Label2.Text = @"异常！请重试！";
			return;
		}
		String strBarCode = liststr[0];
		String strPWD = tbPassword.Text.Trim();
		bool bNeedPWD = tbPassword.Enabled;
		MsgAccessor msgAcce = new MsgAccessor();
		if (bNeedPWD)
		{
			if(strPWD.Length < 5){
				Label2.Text = @"密码错误！";
				return;
			}
			strPWD = OperateControl.GetMD5String(strPWD);
			TiaoMaRecord tmRec = msgAcce.GetOneTiaoMaRecord(strBarCode, null);
			if(tmRec.PWD != strPWD){
				Label2.Text = @"密码错误！";
				return;
			}
		}
		String strUID = operaterLabel.Text;
		int nOprater;
		int.TryParse(strUID, out nOprater);
		int nRet = msgAcce.PickupGoods(1, nOprater, strBarCode, null, null, null);
		if(nRet == 0){
			tbBarCode.Text = "";
			ControlHideDisable();

			Response.Clear();
			this.ClientScript.RegisterStartupScript(GetType(), @"PICKUPBARCODE", @"<Script Language='JavaScript'>window.alert('提货成功！')</script>");
		}
		else {
			Label2.Text = @"访问出现异常，请重试！";
			return;
		}
	}
	protected void tbCancel_Click(object sender, EventArgs e)
	{
		string	strSaveInfo = lblSaveInfo.Text;
		char[] pSplit = new char[1];
		pSplit[0] = '#';
		string[] liststr = strSaveInfo.Split(pSplit);
		if (liststr.Length >= 2)
		{
			String srcLaunchFile = null;
			if (liststr.Length >= 3)
				srcLaunchFile = liststr[2];

			String strUID = operaterLabel.Text;
			int nOprater;
			int.TryParse(strUID, out nOprater);

			MsgAccessor msgAcce = new MsgAccessor();
			int nRet = msgAcce.RevokePkupGoods(nOprater, liststr[0]);
			if (nRet != 20)
			{
				Label2.Text = @"撤销失败！";
			}
			else
			{
				if (srcLaunchFile == null || srcLaunchFile.Length < 1)
				{
					tbBarCode.Text = "";
					ControlHideDisable();

					Response.Clear();
					this.ClientScript.RegisterStartupScript(GetType(), @"REVOKEBARCODE", @"<Script Language='JavaScript'>window.alert('撤销成功！')</script>");
				}
				else
				{
					Response.Clear();
					Response.Redirect(srcLaunchFile);
				}
			}
		}
	}
}
